package com.nerve.android.cloudoffice.service;

import java.util.Map;

import org.nerve.android.net.NetWorker;

import com.alibaba.fastjson.JSON;
import com.nerve.android.cloudoffice.OfficeApplication;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;

public final class NetService {
	private static NetWorker worker;
	
	private static Reply timeOutReply;
	private static Reply noNetReply;
	
	public static NetWorker getWorker(){
		if(worker == null){
			worker = new NetWorker();
			
			//if the sessioId is not null ,put it into  the netWorker
			if(OfficeApplication.JSESSIONID.trim().length() > 0)
				worker.addSessionId(OfficeApplication.JSESSIONID);
			
			Const.log("创建了一个nerWorker");
		}
		return worker;
	}
	
	/**
	 * @method name: getSessionId
	 * @description: get session id
	 * @return type: String
	 *
	 * @return
	 */
	public static String getSessionId(){
		return getWorker().getCookie("JSESSIONID").toString();
	}
	
	
	/**
	 * @method name: post
	 * @description: 
	 * @return type: Reply
	 *
	 * @param url
	 * @param map
	 * @return
	 */
	public static Reply post(String url, Map<String, String> map){
		if(!NetWorker.isConnect(OfficeApplication.context)){
			return getNoNetConnectReply();
		}
		
		String result = getWorker().postData(url, map);
		Const.log("server:" + result);
		if(result.equals(NetWorker.TIME_OUT)){
			Const.log("*******************************************************");
			Const.log("*********************网络超时********************");
			Const.log("*******************************************************");
			return getTimeoutReply();
		}
		
		try{
			Reply re = JSON.parseObject(result, Reply.class);
			
			return re;
		}catch(Exception e){
			Reply re = new Reply();
			re.error(e);
			e.printStackTrace(System.out);
			
			return re;
		}
	}
	
	/**
	 * @method name: get
	 * @description: 
	 * @return type: Reply
	 *
	 * @param url
	 * @return
	 */
	public synchronized static Reply get(String url){
		if(!NetWorker.isConnect(OfficeApplication.context)){
			return getNoNetConnectReply();
		}
		
		String result = getWorker().getData(url);
		Const.log("server from:" + url);
		Const.log("server get:" + result);
		
		if(result.equals(NetWorker.TIME_OUT))
			return getTimeoutReply();
		
		try{
			Reply re = JSON.parseObject(result, Reply.class);
			return re;
		}catch(Exception e){
			Reply re = new Reply();
			re.error(e);
			e.printStackTrace(System.out);
			return re;
		}
	}
	
	private static Reply getTimeoutReply(){
		if(timeOutReply == null){
			timeOutReply = new Reply();
			timeOutReply.setError(true);
			timeOutReply.setMessage("网络连接超时，请稍候重试");
		}
		
		return timeOutReply;
	}
	
	private static Reply getNoNetConnectReply(){
		if(noNetReply == null){
			noNetReply = new Reply();
			noNetReply.setError(true);
			noNetReply.setMessage("无法链接到网络，请检查网络配置");
		}
		
		return noNetReply;
	}
}